package com.zlzkj.app.controller;

//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authz.annotation.RequiresRoles;
//import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
//import org.springframework.web.multipart.MultipartFile;
//import org.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//import java.io.*;
import java.util.HashMap;
import java.util.Map;

//import com.alibaba.fastjson.JSONObject;
import com.zlzkj.core.base.BaseController;
//import com.zlzkj.core.sql.Row;
import com.zlzkj.app.aop.Log;
import com.zlzkj.app.model.Warning;
//import com.zlzkj.app.service.EnergytypeService;
//import com.zlzkj.app.service.RoleService;
//import com.zlzkj.app.service.ShiroUserService;
//import com.zlzkj.app.service.MonitorService;
import com.zlzkj.app.service.UserService;
import com.zlzkj.app.service.WarningService;
import com.zlzkj.app.util.MapUtil;
import com.zlzkj.app.util.PageView;

@Controller
@RequestMapping(value = "/warning")
public class WarningController extends BaseController {

    @Value("${PAGE_SIZE}")
    private int PAGE_SIZE;

    @Value("${FILE_REPOSITORY}")
    private String FILE_REPOSITORY;

    @Autowired
    private WarningService warningService;
    
    @Autowired
    private UserService userService;

//    @Autowired
//    private RoleService roleService;

//    @Autowired
//    private ShiroUserService shiroUserService;

//    @Autowired
//    private EnergytypeService energytypeService;

    @RequestMapping(value = {"/index"})
    public String index(Model model, HttpServletRequest request, HttpServletResponse response, Integer nowPage) {
        Map map = MapUtil.convert(request.getParameterMap());
        model.addAttribute("pageView", new PageView(warningService.findByMap(map, nowPage)));
        return "warning/index";
    }
    
    @Log(name="新增告警详情数据")
    @RequestMapping(value = {"/create"}, method = RequestMethod.POST)
    public String create(Model model, HttpServletRequest request, HttpServletResponse response, Warning warning) {
        return ajaxReturn(response, warningService.save(warning));
    }

    @ResponseBody
    @Log(name="删除告警详情数据")
    @RequestMapping(value = {"/delete"}, method = RequestMethod.POST)
    public String delete(Model model, HttpServletRequest request, HttpServletResponse response, String[] id) {
        return ajaxReturn(response, warningService.deletebyIds(id));
    }
    
    @Log(name="修改告警详情数据")
    @RequestMapping(value = {"/update"}, method = RequestMethod.POST)
    public String edit(Model model, HttpServletRequest request, HttpServletResponse response, Warning warning) {
        return ajaxReturn(response, warningService.update(warning));
    }

    //json 告警列表
    @ResponseBody
    @Log(name="查看告警详情数据")
    @RequestMapping(value = {"/list"}, method = RequestMethod.POST)
    public Map<String, Object> wanyingList(HttpServletRequest request, Integer nowPage) {
        Map<String, Object> result = new HashMap();
        Map map = MapUtil.convert(request.getParameterMap());
        result.put("pageView", new PageView(warningService.findByMap(map, nowPage)));
        return result;
    }

    @ResponseBody
    @RequestMapping(value = {"/to_create"}, method = RequestMethod.POST)
    public Map<String, Object> toCreate(HttpServletRequest request) {
        Map<String, Object> result = new HashMap();
        //result.put("energy", energytypeService.findByMap(new HashMap<String, Object>()));
        return result;
    }

    @ResponseBody
    @RequestMapping(value = {"/to_edit"}, method = RequestMethod.POST)
    public Map<String, Object> toEdit(HttpServletRequest request, String id) {
        Map<String, Object> result = new HashMap();
        //result.put("roles", roleService.findByMap(new HashMap<String, Object>()));
        result.put("obj", warningService.findById(id));
        return result;
    }
    
    @ResponseBody
    @RequestMapping(value = "/check_username")
    public boolean checkAccount(String name, String originalname, HttpSession session) {
        Map<String, Object> paraMap = new HashMap();
        paraMap.put("username", name);
        if (userService.findByMap(paraMap).size() > 0 && !name.equals(originalname)) {
            return true;
        }
        return false;
    }

}
